package com.naiyumi.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.naiyumi.model.UserInfo;
import com.naiyumi.service.impl.DownloadServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope
public class DownloadAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public List<Map<String, Object>> jsonList;
	public Map<String, Object> jsonMap;
	private String node;
	
	private String fileName;
	
	private String admin;
	
	@Autowired
	private DownloadServiceImpl downloadService;

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		try {
			this.fileName = java.net.URLEncoder.encode(fileName,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.fileName = fileName;
		}
	}

	public InputStream getDownloadFile() throws FileNotFoundException {
		//获取相对路径下的文件
		//return ServletActionContext.getServletContext().getResourceAsStream("/" + fileName);
		HttpServletRequest request = ServletActionContext.getRequest();
		String filePath = request.getParameter("filePath").toString();
		System.out.println(filePath);
//		String realPath=ServletActionContext.getServletContext().getRealPath("voice");
//		System.out.println(realPath);
//		try {
//			filePath = new String(filePath.getBytes("ISO-8859-1"),"UTF-8");
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//		}
		System.out.println(filePath);
		File file = new File(filePath);
		boolean flag = file.isFile();
		System.out.println(flag);
		int endIndex = filePath.lastIndexOf(File.separator);
		this.setFileName(filePath.substring(endIndex+1, filePath.length()));
		return new FileInputStream(file);
	}

	public String execute() {
		return "success";
	}
	
	public String treeNode() {
		try {
			UserInfo userInfo = (UserInfo) ActionContext.getContext().getSession().get("userInfo");
			System.out.print(userInfo.getUserName());
			HttpServletRequest request = ServletActionContext.getRequest();
			String path = request.getParameter("path");
			jsonList = this.downloadService.selectTreeNode(this.node,path,userInfo);
			System.out.println(jsonList);
		} catch (Exception e) {
			e.printStackTrace();
			return "input";
		}
		return "success";
	}
	
	public String treeNodeClick(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String path = request.getParameter("path");
		if(path == null){
			return "success";
		}
		System.out.println(path);
		jsonMap = new HashMap<String,Object>();
		List<Map<String,Object>> rows = new ArrayList<Map<String,Object>>();
		rows = this.downloadService.selectFileList(path);
		jsonMap.put("rows", rows);
		System.out.println(jsonMap);
		return "success";
	}
	
	public String floderPassword(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String floder = request.getParameter("floder");
		String password = request.getParameter("password");
		boolean pass = this.downloadService.getPasswordByFloder(floder,password); 
		String msg = "";
		if(pass){
			msg = "{success:true,msg:'成功'}";
		} else {
			msg = "{success:false}";
		}
		
		
		//回复消息
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8"); 
        PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(msg);
			out.flush();
	        out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	public String setFloderPassword(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String floder = request.getParameter("floder");
		String password = request.getParameter("password");
		int count = this.downloadService.setPasswordByFloder(floder,password); 
		String msg = "";
		if(count > 0){
			msg = "{success:true,msg:'密码设置成功!'}";
		} else {
			msg = "{success:false},msg:'密码设置失败!'";
		}
		
		//回复消息
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8"); 
        PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(msg);
			out.flush();
	        out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "success";
	}

	public String getNode() {
		return node;
	}

	public void setNode(String node) {
		this.node = node;
	}

	public List<Map<String, Object>> getJsonList() {
		return jsonList;
	}

	public void setJsonList(List<Map<String, Object>> jsonList) {
		this.jsonList = jsonList;
	}

	public Map<String, Object> getJsonMap() {
		return jsonMap;
	}

	public void setJsonMap(Map<String, Object> jsonMap) {
		this.jsonMap = jsonMap;
	}

	public String getAdmin() {
		return admin;
	}

	public void setAdmin(String admin) {
		this.admin = admin;
	}

}